//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "CDBRGrp.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck est utilisé pour être sûr que les composants créés ne
// contiennent aucune fonction virtuelle pure.
//

static inline void ValidCtrCheck(CDBRadioGroup *)
{
    new CDBRadioGroup(NULL);
}
//---------------------------------------------------------------------------
__fastcall CDBRadioGroup::CDBRadioGroup(TComponent* Owner)
    : TDBRadioGroup(Owner)
{
  m_asRefWebHelp = EmptyStr;
  m_ReadOnly     = false;
  m_Enabled      = true;
  m_Reference    = EmptyStr;
}
//---------------------------------------------------------------------------
void __fastcall CDBRadioGroup::SetEnabled(bool value)
{
  m_Enabled =  value;
  TDBRadioGroup::Enabled = (m_Enabled && !m_ReadOnly);
  Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall CDBRadioGroup::SetReadOnly(bool value)
{
  m_ReadOnly            =  value;
  TDBRadioGroup::ReadOnly =  value;
  TDBRadioGroup::Enabled = (m_Enabled && !m_ReadOnly);
  Invalidate();
}
//---------------------------------------------------------------------------
AnsiString __fastcall CDBRadioGroup::GetReference( void)
{
  return Reference;
}
//---------------------------------------------------------------------------
void __fastcall CDBRadioGroup::CalculateVars(int &BtnPerCol, int &BtnWidth, int &BtnHeight, int &TopMargin)
{
  HDC        DC;
  HFONT      SaveFont;
  TEXTMETRIC Metrics;
  int        i;
  // Code repiqué dans les sources VCL Borland
  // Fonction : TCustomRadioGroup::ArrangeButtons()
  // Permet de déterminer les positions des différents textes
  DC       = ::GetDC(0);
  SaveFont = ::SelectObject(DC, Font->Handle);
  ::GetTextMetrics(DC, &Metrics);
  ::SelectObject(DC, SaveFont);
  ::ReleaseDC(0, DC);

  BtnPerCol = (Items->Count + Columns - 1) / Columns;
  BtnWidth  = (Width - 10) / Columns;
  i         = Height - Metrics.tmHeight - 5;
  BtnHeight = i / BtnPerCol;
  TopMargin = Metrics.tmHeight + 1 + (i % BtnPerCol) / 2;
}
//---------------------------------------------------------------------------
void __fastcall CDBRadioGroup::WMPaint(TWMPaint &Msg)
{
  TDBRadioGroup::Dispatch(&Msg);

  // Vérifie que la liste Items n'est pas vide
  if (!Items->Count) return;
  //if (TDBRadioGroup::Enabled) return;
  if(m_ReadOnly && m_Enabled) {
    HDC   hdc;
    HFONT SaveFont;
    HWND  handle;
    int   BtnPerCol, BtnWidth, BtnHeight, TopMargin;

    CalculateVars(BtnPerCol, BtnWidth, BtnHeight, TopMargin);

    for(int i=0; i<Items->Count; i++) {
      handle   = ::FindWindowEx(Handle, NULL, "TGroupButton", Items->Strings[i].c_str());
                 ::SendMessage(handle, WM_PAINT, 0, 0);
      hdc      = ::GetDC(handle);
      SaveFont = ::SelectObject(hdc, Font->Handle);
                 ::SetBkMode(hdc, TRANSPARENT);

      ::SetTextColor(hdc, ColorToRGB( Color));
      ::TextOut(hdc, 18 + 1, (BtnHeight / 2) - 7 + 1,
                     Items->Strings[i].c_str(), Items->Strings[i].Length());
      ::SetTextColor(hdc, ColorToRGB( Font->Color));
      ::TextOut(hdc, 18    , (BtnHeight / 2) - 7    ,
                     Items->Strings[i].c_str(), Items->Strings[i].Length());


      ::SelectObject(hdc, SaveFont);
      ::ReleaseDC(handle, hdc);
    }
  }
}
//---------------------------------------------------------------------------
namespace Cdbrgrp
{
    void __fastcall PACKAGE Register()
    {
        TComponentClass classes[1] = {__classid(CDBRadioGroup)};
        RegisterComponents("EIC CtrlDB", classes, 0);
    }
}
//---------------------------------------------------------------------------
void __fastcall CDBRadioGroup::KeyDown( Word &Key, TShiftState Shift)
{
      //WebHelp
      //-------
      if( (Key == VK_F3) && (Shift.Contains( ssCtrl )) ) {
            AccesWebHelp( m_asRefWebHelp );
      }else {
          if( Key == VK_F3 ) 
            VerifierRefWebHelp( m_asRefWebHelp );
      }
}
//---------------------------------------------------------------------------
